/**
 * Urban Parks
 *
 * Team M&M's:
 * Michael Satran Jr
 * Michael Pogson
 * Stephen Hunter
 *
 * Date: Feb 16, 2012
 */
package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import model.Job;

import controller.Controller;

/**
 * 
 * @author Michael Pogson, Stephen Hunter, Michael Satran Jr
 */
public class ParkManagerPanel extends UserPanel {
	private JFrame my_job_creation_frame;
	private JFrame my_frame;
	private Controller my_controller;
	private JPanel my_contentPane;
	private JTable my_jobs_table;
	final JList<Job> my_joblist = new JList<Job>();
	
	public ParkManagerPanel(Controller the_controller) {
		my_controller = the_controller;
		my_frame = new JFrame("Urban Parks - Park Manager");
		my_contentPane = new JPanel();
		
	}

	public void start() {
		super.start(my_frame, my_contentPane, my_controller);
		
	}

	@Override
	public void setup() {
		
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_frame.setBounds(100, 100, 450, 300);
		my_contentPane = new JPanel();
		my_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		my_frame.setContentPane(my_contentPane);
		my_contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		my_contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnCreateJob = new JButton("Create New Job");
		
		btnCreateJob.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent the_arg) {
				my_job_creation_frame = new JFrame("Urban Parks: Job Creation");
				my_job_creation_frame.setBounds(100, 100, 450, 300);
				my_job_creation_frame.addWindowListener(new WindowAdapter() {
					public void windowClosed(WindowEvent e) {
						setupTable(my_jobs_table, (List<Job>) my_controller.getJobsForParkManager(), new String[] {"Jobs you are managing"});
					}
				});
				JobRequestPanel panelCreateJob = new JobRequestPanel(my_controller, my_job_creation_frame);
				panelCreateJob.setup();
			}
		});
		panel.add(btnCreateJob);
		
		JButton btnReturnToSign = new JButton("Logout");
		btnReturnToSign.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent the_arg) {
				my_frame.dispose();
			}
		});
		
		JButton btnCalendar = new JButton("View Calendar");
		
		/*Action listener for the view calendar button*/
		btnCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Calendar cal = Calendar.getInstance();
				List<Job>jobs = new ArrayList<Job>(my_controller.getJobsForParkManager());
				
				/*Frame for showing the calendar*/
				try {
					JFrame frame = new JFrame("Calendar");
					CalendarPanel calendar = new CalendarPanel(jobs, cal.get(cal.MONTH)+1 , cal.get(cal.YEAR), frame, my_controller);
					calendar.setup();
					frame.setBounds(100, 100, 450, 300);
					frame.setContentPane(calendar);
					frame.setVisible(true);
					frame.pack();
				} catch(IllegalArgumentException exception) {
					JOptionPane pane = new JOptionPane("No Volunteers!");
					pane.setVisible(true);
				}
			}
			
		});
		
		panel.add(btnCalendar);
		
		panel.add(btnReturnToSign);
		
		my_jobs_table = new JTable();
		setupTable(my_jobs_table, (List<Job>) my_controller.getJobsForParkManager(), new String[] {"Jobs in the system"});
		my_contentPane.add(new JScrollPane(my_jobs_table));
		my_jobs_table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				//Frame for the job information
				JFrame infoframe = new JFrame("Urban Parks: Job Info");
				infoframe.setBounds(100, 100, 450, 300);
				JobInformationPanel infopanel = new JobInformationPanel((Job)my_jobs_table.getValueAt(my_jobs_table.getSelectedRow(), 0), my_controller);
				infopanel.setup();
				infoframe.add(infopanel);
				infoframe.setVisible(true);
				infoframe.pack();
			}
		});
	}

}
